<template>
    <div>
        <div>
            <div style="display: flex;justify-content: space-between">
                <div>
                    <el-input placeholder="请输入车辆品牌进行搜索，可以直接回车搜索..." prefix-icon="el-icon-search"
                              clearable
                              @clear="initCars"
                              style="width: 350px;margin-right: 10px" v-model="keyword"
                              @keydown.enter.native="initCars" :disabled="showAdvanceSearchView"></el-input>
                    <el-button icon="el-icon-search" type="primary" @click="initCars" :disabled="showAdvanceSearchView">
                        搜索
                    </el-button>
                </div>
                <div>
                    <el-button type="primary" icon="el-icon-plus" @click="showAddCarView">
                        添加车辆
                    </el-button>
                </div>
            </div>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="cars"
                    stripe
                    border
                    v-loading="loading"
                    element-loading-text="正在加载..."
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        align="left"
                        fixed
                        label="车辆ID"
                        width="170">
                </el-table-column>
                <el-table-column
                        prop="model"
                        label="品牌型号"
                        fixed
                        align="left"
                        width="85">
                </el-table-column>
                <el-table-column
                        prop="insstatu"
                        width="120"
                        align="left"
                        label="保险情况">
                </el-table-column>
                <el-table-column
                        prop="yearstatu"
                        width="70"
                        label="年检情况">
                </el-table-column>
                <el-table-column
                        prop="mileage"
                        width="110"
                        label="总里程">
                </el-table-column>
                <el-table-column
                        prop="spend"
                        width="140"
                        label="总花销￥">
                </el-table-column>
                <el-table-column
                        prop="borrow"
                        width="100"
                        label="借用情况">
                    <template slot-scope="scope">
                        <span v-if="scope.row.borrow===0">未借出</span>
                        <span v-if="scope.row.borrow===1" style="color: #b2e281">已借出</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        width="160"
                        label="录入时间">
                </el-table-column>
                <el-table-column
                        prop="createBy"
                        width="180"
                        align="left"
                        label="录入人">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        width="160"
                        label="最近更新时间">
                </el-table-column>
                <el-table-column
                        prop="updateBy"
                        width="160"
                        label="更新人">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        width="240"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button @click="showEditCarView(scope.row)" style="padding: 3px" size="mini">编辑</el-button>
                        <el-button style="padding: 3px" size="mini" type="primary" @click="LookManager(scope.row)">
                            所属管理人
                        </el-button>
                        <el-button v-if="!scope.row.borrow" style="padding: 3px" size="mini" type="primary" @click="borrowCar(scope.row)">
                            车辆借出
                        </el-button>
                        <el-button @click="deleteCar(scope.row)" style="padding: 3px" size="mini" type="danger">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @current-change="currentChange"
                        @size-change="sizeChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>
        </div>
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="80%">
            <div>
                <el-form :model="car" :rules="rules" ref="carForm">
                    <el-row>
                        <el-col :span="5">
                            <el-form-item label="车辆品牌:" prop="model">
                                <el-input size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                          v-model="car.model"
                                          placeholder="请输入车辆品牌"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="请选择车险情况:" prop="insstatu">
                                <el-select v-model="car.insstatu" placeholder="请选择">
                                    <el-option
                                            v-for="item in status"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="请选择年检情况:" prop="yearstatu">
                                <el-select v-model="car.yearstatu" placeholder="请选择">
                                    <el-option
                                            v-for="item in status"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="总里程:" prop="mileage">
                                <el-input size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                          v-model="car.mileage"
                                          placeholder="请输入总里程"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="总花销:" prop="cost">
                                <el-input size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                          v-model="car.spend"
                                          placeholder="请输入总花销"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="借出状态:" prop="borrow">
                                <el-select v-model="car.borrow" placeholder="请选择">
                                    <el-option
                                            v-for="item in borrows"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="doAddCar">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="该车辆的投保情况"
                :visible.sync="dialogVisible2"
                width="100%"
                center>
            <div style="margin-top: 10px">
                <el-table
                        :data="lookInses"
                        stripe
                        border
                        v-loading="loading"
                        element-loading-text="正在加载..."
                        element-loading-spinner="el-icon-loading"
                        element-loading-background="rgba(0, 0, 0, 0.8)"
                        style="width: 100%">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="id"
                            align="left"
                            fixed
                            label="承保单号"
                            width="200">
                    </el-table-column>
                    <el-table-column
                            prop="carbrand"
                            label="品牌型号"
                            width="85">
                    </el-table-column>
                    <el-table-column
                            prop="carmaster"
                            width="110"
                            fixed="right"
                            label="车主姓名">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            width="120"
                            label="险种姓名">
                    </el-table-column>
                    <el-table-column
                            prop="finalprice"
                            width="120"
                            label="应付价格">
                    </el-table-column>
                    <el-table-column
                            prop="accept"
                            width="120"
                            label="申请状态">
                        <template slot-scope="scope">
                            <span v-if="scope.row.accept===0">未处理</span>
                            <span v-if="scope.row.accept===1" style="color: #b2e281">同意</span>
                            <span v-if="scope.row.accept===2" style="color: #e30007">拒绝</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="createTime"
                            width="160"
                            label="投保时间">
                    </el-table-column>
                    <el-table-column
                            prop="createBy"
                            width="140"
                            label="投保操作人">
                    </el-table-column>
                    <el-table-column
                            prop="updateTime"
                            width="200"
                            fixed="right"
                            label="最新操作时间">
                    </el-table-column>
                    <el-table-column
                            prop="updateBy"
                            width="200"
                            fixed="right"
                            label="操作人">
                    </el-table-column>
                </el-table>
                <div style="display: flex;justify-content: flex-end">
                    <el-pagination
                            background
                            @current-change="currentChange"
                            @size-change="sizeChange"
                            layout="sizes, prev, pager, next, jumper, ->, total, slot"
                            :total="total">
                    </el-pagination>
                </div>
            </div>
        </el-dialog>
<!--        出借车辆-->
        <el-dialog
            :title="title"
            :visible.sync="dialogVisible3"
            width="20%">
        <div>
            <el-form :model="applyIns" :rules="rules" ref="borrowForm">
                <el-row>
                    <el-form-item label="请选择借车人:" prop="id">
                        <el-select v-model="borrowCars.uId" placeholder="请选择">
                            <el-option
                                    v-for="item in options"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="请输入借车时长:">
                        <el-input type="number" size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                  v-model="borrowCars.duration"
                                  placeholder="请输入借车时长"></el-input>
                        <span>天</span>
                    </el-form-item>
                    <el-form-item label="请输入预计里程:">
                        <el-input type="number" size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                  v-model="borrowCars.mileage"
                                  placeholder="请输入预计里程"></el-input>
                        <span>公里</span>
                    </el-form-item>
                    <el-form-item label="请输入预计花销:">
                        <el-input type="number" size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                  v-model="borrowCars.spend"
                                  placeholder="请输入预计花销"></el-input>
                        <span>元</span>
                    </el-form-item>

                </el-row>

            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
                <el-button @click=" dialogVisible3= false">取 消</el-button>
                <el-button type="primary" @click="borrow()">申请</el-button>
            </span>
    </el-dialog>

<!--        // 查看所属管理人-->
        <el-dialog
                :title="title"
                :visible.sync="dialogVisibleMana"
                width="55%">
            <el-table
                    :data="carManagers"
                    stripe
                    border
                    v-loading="loading"
                    element-loading-text="正在加载..."
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="width: 100%">
                <el-table-column
                        prop="id"
                        label="人员编号"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="mname"
                        label="姓名"
                        width="85">
                </el-table-column>
                <el-table-column
                        prop="age"
                        width="90"
                        label="年龄">
                </el-table-column>
                <el-table-column
                        prop="sex"
                        width="120"
                        label="性别">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        width="160"
                        label="入职时间">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        width="160"
                        label="分配时间">
                </el-table-column>
                <el-table-column
                        prop="createBy"
                        width="109"
                        label="分配人">
                </el-table-column>
            </el-table>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "CarInfo",
        data() {
            return {
                searchValue: {

                    model: null,

                },
                title: '',
                importDataBtnIcon: 'el-icon-upload2',
                importDataDisabled: false,
                showAdvanceSearchView: false,
                allDeps: [],
                cars: [],
                borrowCars: {
                    id:"",
                    uId:"",
                    cId:"",
                    name:"",
                    duration:0,
                    spend:0.00,
                    mileage:0.00
                },
                applyIns: {
                    id: "",
                    carId: "",
                    insId: "",
                    duration: 0,
                    basePrice: 0.00,
                    finalPrice: 0.00,
                },
                tip: "",
                loading: false,
                popVisible: false,
                popVisible2: false,
                dialogVisible: false,
                dialogVisible2: false,
                dialogVisible3: false,
                dialogVisibleMana: false,
                lookInses: [],
                total: 0,
                page: 1,
                keyword: '',
                size: 10,
                carManagers:[],
                tiptopDegrees: ['本科', '大专', '硕士', '博士', '高中', '初中', '小学', '其他'],
                options: [],
                status: [{
                    name: "正常",
                    id: "正常"
                }, {
                    name: "不正常"
                    , id: "不正常"
                }],
                borrows: [{
                    name: "未借出",
                    id: 0
                }, {
                    name: "已借出"
                    , id: 1
                }],
                inputDepName: '所属部门',
                car: {
                    id: "",
                    model: "X5",
                    insstatu: "正常",
                    yearstatu: "正常",
                    mileage: 0,
                    borrow: "未借出",
                    spend: 0.00
                },
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                rules: {
                    model: [{required: true, message: '请输入车辆型号', trigger: 'blur'}],


                }
            }
        },
        mounted() {
            this.initCars();
            // this.initData();
            // this.initPositions();
        },
        methods: {
            searvhViewHandleNodeClick(data) {
                this.inputDepName = data.name;
                this.searchValue.departmentId = data.id;
                this.popVisible2 = !this.popVisible2
            },
            onError(err, file, fileList) {
                this.importDataBtnText = '导入数据';
                this.importDataBtnIcon = 'el-icon-upload2';
                this.importDataDisabled = false;
            },
            onSuccess(response, file, fileList) {
                this.importDataBtnText = '导入数据';
                this.importDataBtnIcon = 'el-icon-upload2';
                this.importDataDisabled = false;
                this.initEmps();
            },
            beforeUpload() {
                this.importDataBtnText = '正在导入';
                this.importDataBtnIcon = 'el-icon-loading';
                this.importDataDisabled = true;
            },
            exportData() {
                window.open('/employee/basic/export', '_parent');
            },
            emptyCar() {
                this.car = {
                    id: "",
                    brand: "",
                    model: "",
                    address: "",
                    telphone: null,
                    carmaster: "",
                    price: 0.00,
                    enginenum: "",
                }
            },
            showEditCarView(data) {
                // this.initPositions();
                this.title = '编辑车辆信息';
                this.car = data;
                this.dialogVisible = true;
            },
            borrow() {
                this.borrowCars.cId = this.car.id;
                console.log("borrowInfo:", this.borrowCars);
                this.$refs['borrowForm'].validate(valid => {
                    if (valid) {
                        this.postRequest("/apply/", this.borrowCars).then(resp => {
                            if (resp) {
                                this.dialogVisible3 = false;
                                this.initCars();
                            }
                        })
                    }
                });
            },
            borrowCar(data) {
                this.dialogVisible3 = true;
                this.applyIns = {
                    id: "",
                    cId: "",
                    uId: "",
                    duration: 0.00,
                    mileage: 0.00,
                    spend: 0.00
                };
                this.car = data;
                this.getRequest("/apply/userList/").then(resp => {
                    if (resp) {
                        this.options = resp;
                    }
                })
            },
            LookManager(data) {
                this.dialogVisibleMana = true;
                this.DialogDataLoading(data.id);
            },
            DialogDataLoading(data) {
                this.postRequest("/manager/basic/" + data).then(resp => {
                    if (resp) {
                        this.carManagers = resp;
                    }
                })
            },
            deleteCar(data) {
                this.$confirm('此操作将永久删除【' + data.id + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/car/basic/" + data.id).then(resp => {
                        if (resp) {
                            this.initCars();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAddCar() {
                if (this.car.id) {
                    this.$refs['carForm'].validate(valid => {
                        if (valid) {
                            this.putRequest("/car/basic/", this.car).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initCars();
                                }
                            })
                        }
                    });
                } else {
                    this.$refs['carForm'].validate(valid => {
                        if (valid) {
                            this.postRequest("/car/basic/", this.car).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initCars();
                                }
                            })
                        }
                    });
                }
            },
            handleNodeClick(data) {
                this.inputDepName = data.name;
                this.emp.departmentId = data.id;
                this.popVisible = !this.popVisible
            },
            showDepView() {
                this.popVisible = !this.popVisible
            },
            showDepView2() {
                this.popVisible2 = !this.popVisible2
            },
            initPositions() {
                this.getRequest('/employee/basic/positions').then(resp => {
                    if (resp) {
                        this.positions = resp;
                    }
                })
            },
            getMaxWordID() {
                this.getRequest("/employee/basic/maxWorkID").then(resp => {
                    if (resp) {
                        this.emp.workID = resp.obj;
                    }
                })
            },
            initData() {
                if (!window.sessionStorage.getItem("nations")) {
                    this.getRequest('/car/basic/nations').then(resp => {
                        if (resp) {
                            this.nations = resp;
                            window.sessionStorage.setItem("nations", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.nations = JSON.parse(window.sessionStorage.getItem("nations"));
                }
                if (!window.sessionStorage.getItem("joblevels")) {
                    this.getRequest('/employee/basic/joblevels').then(resp => {
                        if (resp) {
                            this.joblevels = resp;
                            window.sessionStorage.setItem("joblevels", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.joblevels = JSON.parse(window.sessionStorage.getItem("joblevels"));
                }
                if (!window.sessionStorage.getItem("politicsstatus")) {
                    this.getRequest('/employee/basic/politicsstatus').then(resp => {
                        if (resp) {
                            this.politicsstatus = resp;
                            window.sessionStorage.setItem("politicsstatus", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.politicsstatus = JSON.parse(window.sessionStorage.getItem("politicsstatus"));
                }
                if (!window.sessionStorage.getItem("deps")) {
                    this.getRequest('/employee/basic/deps').then(resp => {
                        if (resp) {
                            this.allDeps = resp;
                            window.sessionStorage.setItem("deps", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.allDeps = JSON.parse(window.sessionStorage.getItem("deps"));
                }
            },
            sizeChange(currentSize) {
                this.size = currentSize;
                this.initEmps();
            },
            currentChange(currentPage) {
                this.page = currentPage;
                this.initEmps();
            },
            showAddCarView() {
                this.emptyCar();
                this.title = '添加车辆';
                this.dialogVisible = true;
            },
            initCars(type) {
                this.loading = true;
                let url = '/car/basic/?page=' + this.page + '&size=' + this.size;

                url += "&model=" + this.keyword;

                this.getRequest(url).then(resp => {
                    this.loading = false;
                    if (resp) {
                        this.cars = resp.data;
                        this.total = resp.total;
                    }
                });
            }
        }
    }
</script>

<style>
    /* 可以设置不同的进入和离开动画 */
    /* 设置持续时间和动画函数 */
    .slide-fade-enter-active {
        transition: all .8s ease;
    }

    .slide-fade-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }

    .slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */
    {
        transform: translateX(10px);
        opacity: 0;
    }
</style>